/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package radiostation;

import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.SimpleDateFormat;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import model.Album;
import model.Song;
/**
 * @author stamatelopoulos
 */
public class EditMusicGroupAlbumForm extends javax.swing.JFrame {
 
    /**
     * Creates new form EditArtistAlbumForm
     */
    public static SimpleDateFormat dateFormater = new SimpleDateFormat("dd/MM/yyyy");
    int availableSongRow;
    int selectedSongRow;        
    Album album;
    Song song;

    public EditMusicGroupAlbumForm(Album alb, boolean readOnly) {  
      
        em = DBManager.em;
        this.readOnly = readOnly;
        this.alb = alb;
        if ( !(em.getTransaction().isActive()) ){
            em.getTransaction().begin();
        }
        initComponents();       
        DBManager.centerOnScreen(this);
        this.availableTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.selectedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
        TableCellRenderer rendererFromHeader = availableTable.getTableHeader().getDefaultRenderer();
        JLabel Id = (JLabel)rendererFromHeader;
        Id.setHorizontalAlignment(JLabel.CENTER);
        
        TableCellRenderer rendererFromHeader2 = selectedTable.getTableHeader().getDefaultRenderer();
        JLabel firstName = (JLabel)rendererFromHeader2;
        firstName.setHorizontalAlignment(JLabel.CENTER);

        JTableHeader header = availableTable.getTableHeader();
        header.setForeground(Color.red);
        
        JTableHeader header2 = selectedTable.getTableHeader();
        header2.setForeground(Color.red);

        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        availableTable.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
        availableTable.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);
        selectedTable.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
        selectedTable.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);
       
        if (readOnly == false){ //add new album
            System.out.println("new album");
            SelectedSongList.clear();
        }
        else { //edit existing musicgroup
            System.out.println("edit existing album");
            jDateChooser1.setDate(album1.getReleasedate());
            AvailableSongList.clear();
            AvailableSongList.addAll(AvailableSongQuery.getResultList());
            SelectedSongList.clear();  
             System.out.println("no" + alb.getSongList().size()); 
          
        for (Song song : alb.getSongList()){
                SelectedSongList.add(song);
                AvailableSongList.remove(song);
            }
        }                                                                          

    }
       
      private void manageButtons() {
        availableSongRow = availableTable.getSelectedRow();
        selectedSongRow = selectedTable.getSelectedRow();
        addButton.setEnabled(availableSongRow >= 0);
        removeButton.setEnabled(selectedSongRow >= 0);
    }
     public EditMusicGroupAlbumForm() {
        initComponents();
   }   
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        album1 = alb;
        song1 = s;
        musicproductioncompanyQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT m FROM Musicproductioncompany m ORDER BY m.name");
        musicproductioncompanyList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : musicproductioncompanyQuery.getResultList();
        MusicGroupQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT m FROM Musicgroup m ORDER BY m.name");
        MusicgroupList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : MusicGroupQuery.getResultList();
        SelectedSongQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT s FROM Song s ORDER BY s.trackno");
        AvailableSongQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT s FROM Song s WHERE s.falbumId IS NULL ORDER BY s.title");
        AvailableSongList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(AvailableSongQuery.getResultList());
        SelectedSongList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(SelectedSongQuery.getResultList());
        artistBoxRenderer1 = new radiostation.ArtistBoxRenderer();
        musicCompanyBoxRenderer1 = new radiostation.MusicCompanyBoxRenderer();
        musicgroupBoxRenderer1 = new radiostation.MusicgroupBoxRenderer();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        DiskNoSpinner = new javax.swing.JSpinner();
        ArtistComboBox = new javax.swing.JComboBox();
        TypeComboBox = new javax.swing.JComboBox();
        MPCompanyComboBox = new javax.swing.JComboBox();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        availableTable = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        selectedTable = new javax.swing.JTable();
        InsertSongjButton = new javax.swing.JButton();
        DeleteSongButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        artistBoxRenderer1.setText("artistBoxRenderer1");

        musicCompanyBoxRenderer1.setText("musicCompanyBoxRenderer1");

        musicgroupBoxRenderer1.setText("musicgroupBoxRenderer1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Διαχείριση Άλμπουμ Συγκροτημάτων");

        jLabel1.setForeground(new java.awt.Color(0, 51, 255));
        jLabel1.setText("Στοιχεία Άλμπουμ:");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName(""); // NOI18N

        jLabel2.setText("Κωδικός:");
        jLabel2.setEnabled(false);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Τίτλος:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Ημερ. Κυκλοφορίας:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Συγκρότημα:");

        jTextField1.setEditable(false);
        jTextField1.setForeground(new java.awt.Color(204, 204, 204));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, album1, org.jdesktop.beansbinding.ELProperty.create("${id}"), jTextField1, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jTextField2.setToolTipText("Πληκτρολογείστε τον Τίτλο του Άλμπουμ");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, album1, org.jdesktop.beansbinding.ELProperty.create("${title}"), jTextField2, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel8.setText("Εταιρεία Παραγωγής:");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel10.setText("Τύπος:");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel11.setText("Αρ. Δίσκων:");

        DiskNoSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
        DiskNoSpinner.setToolTipText("Επιλέξτε τον αριθμό των δίσκων του Άλμπουμ");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, album1, org.jdesktop.beansbinding.ELProperty.create("${disknumber}"), DiskNoSpinner, org.jdesktop.beansbinding.BeanProperty.create("value"));
        binding.setSourceNullValue(0);
        binding.setSourceUnreadableValue(0);
        bindingGroup.addBinding(binding);

        ArtistComboBox.setToolTipText("Επιλέξτε το Συγκρότημα του Άλμπουμ");
        ArtistComboBox.setRenderer(musicgroupBoxRenderer1);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, MusicgroupList, ArtistComboBox);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, album1, org.jdesktop.beansbinding.ELProperty.create("${fgroupId}"), ArtistComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        TypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CD", "EP", "LP" }));
        TypeComboBox.setToolTipText("Επιλέξτε τον Τύπο του Άλμπουμ");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, album1, org.jdesktop.beansbinding.ELProperty.create("${type}"), TypeComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        MPCompanyComboBox.setToolTipText("Επιλέξτε την Εταιρία Παραγωγής του Άλμπουμ");
        MPCompanyComboBox.setRenderer(musicCompanyBoxRenderer1);

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicproductioncompanyList, MPCompanyComboBox);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, album1, org.jdesktop.beansbinding.ELProperty.create("${flabelId}"), MPCompanyComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        jDateChooser1.setToolTipText("Επιλέξτε την Ημερομηνία Κυκλοφορίας");
        jDateChooser1.setDateFormatString("d MMMM yyyy");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ArtistComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TypeComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DiskNoSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(MPCompanyComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(DiskNoSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MPCompanyComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ArtistComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel6.setForeground(new java.awt.Color(0, 51, 255));
        jLabel6.setText("Διαθέσιμα Τραγούδια: {δεν ανήκουν σε κανένα Άλμπουμ}    ");

        jLabel7.setForeground(new java.awt.Color(0, 51, 255));
        jLabel7.setText("Τραγούδια Άλμπουμ:");

        addButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/next.png"))); // NOI18N
        addButton.setText("Προσθήκη");
        addButton.setToolTipText("Πατήστε για Προσθήκη του Τραγουδιού στο Άλμπουμ");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/previous.png"))); // NOI18N
        removeButton.setText("Αφαίρεση");
        removeButton.setToolTipText("Πατήστε για Αφαίρεση του Τραγουδιού από το Άλμπουμ");
        removeButton.setMaximumSize(new java.awt.Dimension(107, 31));
        removeButton.setMinimumSize(new java.awt.Dimension(107, 31));
        removeButton.setPreferredSize(new java.awt.Dimension(107, 31));
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, AvailableSongList, availableTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${trackno}"));
        columnBinding.setColumnName("Track No");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Τίτλος");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duration}"));
        columnBinding.setColumnName("Διάρκεια {sec}");
        columnBinding.setColumnClass(Integer.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        availableTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                availableTableMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(availableTable);
        if (availableTable.getColumnModel().getColumnCount() > 0) {
            availableTable.getColumnModel().getColumn(0).setMaxWidth(60);
            availableTable.getColumnModel().getColumn(1).setMaxWidth(230);
            availableTable.getColumnModel().getColumn(2).setMaxWidth(90);
        }

        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, SelectedSongList, selectedTable);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${trackno}"));
        columnBinding.setColumnName("Track No");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Τίτλος");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duration}"));
        columnBinding.setColumnName("Διάρκεια {sec}");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        selectedTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                selectedTableMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(selectedTable);
        if (selectedTable.getColumnModel().getColumnCount() > 0) {
            selectedTable.getColumnModel().getColumn(0).setMaxWidth(60);
            selectedTable.getColumnModel().getColumn(1).setMaxWidth(230);
            selectedTable.getColumnModel().getColumn(2).setMaxWidth(90);
        }

        InsertSongjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add.gif"))); // NOI18N
        InsertSongjButton.setText("Εισαγωγή Τραγουδιού");
        InsertSongjButton.setToolTipText("Πατήστε για Εισαγωγή νέου Τραγουδιού στον Πίνακα Τραγουδιών");
        InsertSongjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InsertSongjButtonActionPerformed(evt);
            }
        });

        DeleteSongButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/remove.gif"))); // NOI18N
        DeleteSongButton.setText("Διαγραφή Τραγουδιού");
        DeleteSongButton.setToolTipText("Πατήστε για Διαγραφή του Τραγουδιού από τον Πίνακα Τραγουδιών");
        DeleteSongButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteSongButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(InsertSongjButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(DeleteSongButton))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(removeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel7)
                        .addGap(289, 289, 289))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(addButton)
                        .addGap(18, 18, 18)
                        .addComponent(removeButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DeleteSongButton)
                    .addComponent(InsertSongjButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        cancelButton.setText("Ακύρωση");
        cancelButton.setToolTipText("Πατήστε για Ακύρωση και Επιστροφή ");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        saveButton.setText("Αποθήκευση");
        saveButton.setToolTipText("Πατήστε για Αποθήκευση και Επιστροφή");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(cancelButton)
                        .addGap(18, 18, 18)
                        .addComponent(saveButton)
                        .addGap(46, 46, 46))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        // TODO add your handling code here:
        //προσθήκη Τραγουδιού στο Άλμπουμ                
                        
        song = AvailableSongList.get(availableTable.convertRowIndexToModel(availableSongRow));  
        AvailableSongList.remove(song);
        song.setFalbumId(alb);
        SelectedSongList.add(SelectedSongList.size(),song);
        addButton.setEnabled(false);                
                                        
    }//GEN-LAST:event_addButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        // TODO add your handling code here:
        //Αφαίρεση Τραγουδιού απο το Άλμπουμ
                    
        song = SelectedSongList.get(selectedTable.convertRowIndexToModel(selectedSongRow));
        SelectedSongList.remove(song);
        AvailableSongList.add(AvailableSongList.size(),song);
        song.setFalbumId(null);
        removeButton.setEnabled(false);               
        
    }//GEN-LAST:event_removeButtonActionPerformed

    private void availableTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_availableTableMouseClicked
        // TODO add your handling code here:
        selectedTable.clearSelection();
        removeButton.setEnabled(false);
        manageButtons();
    }//GEN-LAST:event_availableTableMouseClicked

    private void selectedTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_selectedTableMouseClicked
        // TODO add your handling code here:
        availableTable.clearSelection();
        addButton.setEnabled(false);
        manageButtons();
    }//GEN-LAST:event_selectedTableMouseClicked

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, false);
        for (WindowListener l : this.getWindowListeners()){
            l.windowClosed(we);
        }
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        // TODO add your handling code here:
        int rows = selectedTable.getRowCount();
        int songflag = 0;
        
            for (Song s1 : SelectedSongList) {
            for (Song s2 : SelectedSongList) {
                if (s1 != s2 && s1.getTitle().equalsIgnoreCase(s2.getTitle())) {                    
                    songflag = 1;
                    break;
                }                   
            }
         if (s1.getDuration() < 0)  {                       
             songflag = 2;
             break;             
            }  
         }
        
        if (jTextField2.getText().isEmpty()  
           || ArtistComboBox.getSelectedIndex() == -1 
           || TypeComboBox.getSelectedIndex() == -1 
           || MPCompanyComboBox.getSelectedIndex() == -1) { 
            JOptionPane.showMessageDialog(null, "Προσοχή υπάρχουν υποχρεωτικά πεδία που δεν έχουν συμπληρωθεί!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE);
        } else if ((Integer) DiskNoSpinner.getValue() < 1 || (Integer) DiskNoSpinner.getValue() > 100) {
             JOptionPane.showMessageDialog(null, "Προσοχή ο αριθμός των Δίσκων παίρνει τιμές απο 1 έως 100!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE);        
        } else if (rows < 1){
            JOptionPane.showMessageDialog(null, "Προσοχή το Άλμπουμ πρέπει να έχει τουλάχιστον 1 Τραγούδι!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE);
        } else if (songflag == 1) {
             JOptionPane.showMessageDialog(this,"Στα Διαθέσιμα τραγούδια υπάρχει ίδιος τίτλος σε πάνω απο μία εγγραφές!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE); 
        } else if (songflag == 2) {
               JOptionPane.showMessageDialog(this,"Στα Διαθέσιμα τραγούδια υπάρχει/ουν εγγραφές με αρνητική διάρκεια (σε sec)!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE);    
        } else { 
            
        album1.setReleasedate(jDateChooser1.getDate());
        
                for (Song song : alb.getSongList()){
                     song = em.merge(song);
                     if (!SelectedSongList.contains(song)){
                      alb.getSongList().remove(song);
                    }
               }
               alb.getSongList().retainAll(SelectedSongList);
               for (Song song : SelectedSongList){
                    song = em.merge(song);
                   if (!alb.getSongList().contains(song)){
                       alb.getSongList().add(song);  
                       song.setFalbumId(alb);
                      }
                }
      
            MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, true);
            for (WindowListener l : this.getWindowListeners()){
                l.windowClosed(we);
            }
            this.setVisible(false);            
       
        }
        
    }//GEN-LAST:event_saveButtonActionPerformed

    private void InsertSongjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertSongjButtonActionPerformed
        // TODO add your handling code here:                                   
        // Δημιουργία νέου τραγουδιού
        int selectedRow = AvailableSongList.size() + 1;
        Song newsong = new Song(null);
       if (selectedRow < 10) { newsong.setTrackno("0"+Integer.toString(selectedRow));}
       else {newsong.setTrackno(Integer.toString(selectedRow));
       }       
        newsong.setTitle("Νέο Τραγούδι " + Integer.toString(selectedRow));
        newsong.setDuration(0);               
        int durationflag = 0;
        
        //Πρόσθεση Τραγουδιού στην AvailableSongList και focus στη νέα γραμμή
        selectedRow =  selectedRow -1;
        AvailableSongList.add(newsong);                        
        availableTable.setRowSelectionInterval(selectedRow,selectedRow);                                       
        
        // Πρόσθεση Τραγουδιού στην Βάση Δεδομένων μετά απο έλεγχο μοναδικότητας στο τίτλο του τραγουδιού        
            for (Song s1 : AvailableSongList) {
            for(Song s2 : AvailableSongList) {
                if (s1 != s2 && s1.getTitle().equalsIgnoreCase(s2.getTitle())) {
                    JOptionPane.showMessageDialog(this,"O Τίτλος του τραγουδιού υπάρχει ήδη.", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE);                    
                    AvailableSongList.remove(newsong);
                    newsong = new Song(null);
                    break;
                }                   
            }
        
        if (s1.getDuration() < 0)  {
             JOptionPane.showMessageDialog(this,"Υπάρχει τραγούδι με αρνητική διάρκεια (σε sec).", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE);                
             durationflag ++;
             break;
         }                              
        
         if (newsong != null && durationflag == 0) { 
            em.persist(newsong);
           }
        }
    }//GEN-LAST:event_InsertSongjButtonActionPerformed

    private void DeleteSongButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteSongButtonActionPerformed
        // TODO add your handling code here:
         
         availableSongRow = availableTable.convertRowIndexToModel(availableSongRow);
        //availableSongRow = availableTable.getSelectedRow();
        if (availableSongRow == -1) {
        JOptionPane.showMessageDialog(this, "Δεν έχει επιλεγεί τραγούδι!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.WARNING_MESSAGE);
        return;        
        }  
        // Επιβεβαίωση Διαγραφής
        int answer = JOptionPane.showConfirmDialog(this,"Το τραγούδι πρόκειται να διαγραφεί. Συμφωνείτε ?","ΕΠΙΒΕΒΑΙΩΣΗ ΔΙΑΓΡΑΦΗΣ",JOptionPane.YES_NO_OPTION);        
        if (answer != 0 )
        return;    
        
        Song tobedeletedsong = AvailableSongList.get(availableSongRow);
        AvailableSongList.remove(tobedeletedsong);
        em.remove(tobedeletedsong);
        
    }//GEN-LAST:event_DeleteSongButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ArtistComboBox;
    private java.util.List<model.Song> AvailableSongList;
    private javax.persistence.Query AvailableSongQuery;
    private javax.swing.JButton DeleteSongButton;
    private javax.swing.JSpinner DiskNoSpinner;
    private javax.swing.JButton InsertSongjButton;
    private javax.swing.JComboBox MPCompanyComboBox;
    private javax.persistence.Query MusicGroupQuery;
    private java.util.List<model.Musicgroup> MusicgroupList;
    private java.util.List<model.Song> SelectedSongList;
    private javax.persistence.Query SelectedSongQuery;
    private javax.swing.JComboBox TypeComboBox;
    private javax.swing.JButton addButton;
    private model.Album album1;
    private radiostation.ArtistBoxRenderer artistBoxRenderer1;
    private javax.swing.JTable availableTable;
    private javax.swing.JButton cancelButton;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private radiostation.MusicCompanyBoxRenderer musicCompanyBoxRenderer1;
    private radiostation.MusicgroupBoxRenderer musicgroupBoxRenderer1;
    private java.util.List<model.Musicproductioncompany> musicproductioncompanyList;
    private javax.persistence.Query musicproductioncompanyQuery;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JTable selectedTable;
    private model.Song song1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
   private EntityManager em;
   private model.Album alb;   
   private model.Song s;
   JFrame thisFrame;
   boolean readOnly;
}
